/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrgs.inf.prav.interop.resource;

import br.ufrgs.inf.prav.interop.resource.ClientType;
import br.ufrgs.inf.prav.interop.util.HTTPClientIdentifier;
import br.ufrgs.inf.prav.interop.util.PathParser;
import java.util.HashSet;
import java.util.Set;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import obai.control.PlataformBean;

/**
 *
 * @author Fernando Arena Varella
 * @version 1.0
 */
public class ResourceFinder {

    public ResourceFinder(){

    }

    public String makeReferenceFromItemID(String itemID){



        return null;
    }


    public String makeFinalReference(String originalSrc){
//        System.out.println("Original Src: " + originalSrc);

        ClientType clientType = HTTPClientIdentifier.getClientType();
        String finalSrc = "";
        finalSrc = buildResourcePath(originalSrc, clientType.toString());

//        System.out.println("Final Src: " + finalSrc);
        return finalSrc;
    }

    /**
     * Build the String of the path to a resource.
     * TODO: arrumar a função para funcionar corretamente quando o caminho
     * original começa por "../"
     * @param original  the original path of the resource
     * @param plataform the target plataform
     * @return  the path to the same resource but under the target plataform
     * directories tree
     */
    private String buildResourcePath(String original, String plataform){
        
        ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
        String alternativeResource = PathParser.concatenateStringToFileName(original, plataform);

        String altResourcePath;
        if (!original.contains("/OBAI")){
            altResourcePath = PathParser.excludeFileName(ctx.getRequestPathInfo());
            String relativePath = plataform + "/" + original;
            altResourcePath += alternativeResource;
            //fix all the "../" ignoring the plataform String
            altResourcePath = PathParser.fixBackwardsDirectories(altResourcePath, plataform);
        } else {
            altResourcePath = original.replace("/OBAI", "");

        }

//        if (altResourcePath.contains("/OBAI")){
//            System.out.println("Original: " + altResourcePath);
//            altResourcePath = altResourcePath.replace("/OBAI", "");
//            System.out.println("path após remoção: " + altResourcePath);
//        }

//        System.out.println("altResourcePath: " + altResourcePath);

        if (resourceExists(altResourcePath))
            return alternativeResource;
        else
            return original;
    }

    /**
     * Test if the given resource exists in the server
     * @param path  the path to the resource
     * @return  true if it exists, false otherwise
     */
    private boolean resourceExists(String path){
        ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
        Set<String> paths = ctx.getResourcePaths(PathParser.excludeFileName(path));
//        for (String s : paths){
//            System.out.println("Resource Path: " + s);
//        }
        if (paths == null)
            paths = new HashSet<String>();

        if (paths.contains(path))
            return true;

//        HttpServletRequest request = (HttpServletRequest)
//                FacesContext.getCurrentInstance().getExternalContext().getRequest();
//        System.out.println("ctx.getRequestServletPath(): " + ctx.getRequestServletPath());
//        System.out.println("ctx.getRequestPathInfo(): " + ctx.getRequestPathInfo());
//        System.out.println("ctx.getRequestContextPath(): " + ctx.getRequestContextPath());
        

        return false;
    }
    

    // <editor-fold defaultstate="collapsed" desc="Getters And Setters">
    // </editor-fold>

}
